﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Kelper.Infrastructure.Helpers
{
    public class Row<T> : IEnumerable<T>
    {
        public IList<T> Items { get; set; }
        public Row() { Items = new List<T>(); }

        public IEnumerator<T> GetEnumerator()
        {
            return Items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

    public class Table<T> : IEnumerable<Row<T>>
    {
        public IList<Row<T>> Rows { get; private set; }

        public Table(IEnumerable<T> items, ushort itemsPerRow = 5)
        {
            Rows = new List<Row<T>>();

            Row<T> currentRow = new Row<T>();
            foreach (var item in items
                .Select((i, index) => new { value = i, index }))
            {
                currentRow.Items.Add(item.value);
                if ((item.index + 1) % itemsPerRow == 0)
                {
                    Rows.Add(currentRow);
                    currentRow = new Row<T>();
                }
            }
            if (currentRow.Items.Count != 0)
                Rows.Add(currentRow);
        }

        public IEnumerator<Row<T>> GetEnumerator()
        {
            return Rows.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}